package com.alibaba.cloud.boot.order.controller;


import org.springframework.cloud.openfeign.TenantThreadLocal;
import com.alibaba.cloud.boot.order.entity.Order;
import com.alibaba.cloud.boot.order.facade.DefaultStockFacade;
import com.alibaba.cloud.boot.order.facade.dubbo.StockService;
import com.alibaba.cloud.boot.order.mapper.OrderMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author xrr
 * @since 2024-02-12
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DefaultStockFacade defaultStockFacade;

    @Resource
    private OrderMapper orderMapper;

    @DubboReference(check = false)
    private StockService dubboStockService;

    @GetMapping("/createOrder")
    public String createOrder() {

        TenantThreadLocal.tenantThreadLocal.set("001");

        Order orderEntity = new Order();
        orderEntity.setOrderId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase(Locale.ROOT));
        orderEntity.setOrderName("orderName:" + orderEntity.getOrderId());
        orderMapper.insert(orderEntity);

        String result = defaultStockFacade.reduceStockCount(1, 1);

        String message = "调用库存服务返回结果:" + result;
        logger.info(message);
        return result;
    }

    @GetMapping("/onlyInvokeAotherServer")
    public String onlyInvokeAotherServer(){
        TenantThreadLocal.tenantThreadLocal.set("001");
        String result = defaultStockFacade.reduceStockCount(1, 1);

        String message = "调用库存服务返回结果:" + result;
        logger.info(message);
        return result;
    }

    @GetMapping("/failInvoke")
    public String failInvoke(){
        for (int i=1 ; i<100; i++){
            int tenantRand = ThreadLocalRandom.current().nextInt();
            if (tenantRand%2 ==1){
                TenantThreadLocal.tenantThreadLocal.set("001");
            }else {
                TenantThreadLocal.tenantThreadLocal.set("002");
            }
            if (i%2 ==1){
                System.out.println(defaultStockFacade.mustFail());
            }else {
                System.out.println(defaultStockFacade.reduceStockCount(1,1));
            }


        }
        return "ok";
    }

    @GetMapping("/dubboCreateOrder")
    public String dubboCreateOrder() {
        Order orderEntity = new Order();
        orderEntity.setOrderId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase(Locale.ROOT));
        orderEntity.setOrderName("orderName:" + orderEntity.getOrderId());
        orderMapper.insert(orderEntity);

        String result = dubboStockService.reduceStockCount(1, 1);
        String message = "调用库存服务返回结果:" + result;
        logger.info(message);
        return result;
    }

}
